<template>
    <div class="app-container">
        <el-card shadow="always">
            <div slot="header" class="clearfix">
                <span> {{$t("tool.nianwen")}}</span>
            </div>
            <el-form ref="setting" :inline="true" label-width="auto"  v-for="(item, index) in settingList" :key="index">
                <el-form-item :label='$t("tool.data") + " " + (index + 1)'>
                </el-form-item>
                <el-form-item :label="$t('tool.temperture')" >
                    <el-input v-model="item.temperture" clearable type="number" style="width: 165px">
                        <i slot="suffix" class="el-input__icon">°C</i>
                    </el-input>
                </el-form-item>
                <el-form-item :label="$t('tool.naindu')" >
                    <el-input v-model="item.niandu" clearable type="number"  style="width: 165px">
                        <i slot="suffix" class="el-input__icon">mpa.s</i>
                    </el-input>
                </el-form-item>
            </el-form>

            <el-form :model="formInline" label-width="auto">
                <el-form-item :label='$t("lab.temperatureScope")'>
                </el-form-item>
                <el-form-item :label="$t('tool.startTemperature')">
                    <el-input v-model="formInline.startTemperature" clearable type="number">
                        <i slot="suffix" class="el-input__icon">°C</i>
                    </el-input>
                </el-form-item>
                <el-form-item :label="$t('tool.endTemperature')" >
                    <el-input v-model="formInline.endTemperature" clearable type="number" >
                        <i slot="suffix" class="el-input__icon">°C</i>
                    </el-input>
                </el-form-item>
            </el-form>
            <el-button-group style="width: 100%">
                <el-button @click="cal" type="primary" style="width: 80%;margin-left: 10%" round size="medium" >
                    {{$t("tool.cal")}}
                </el-button>
            </el-button-group>
        </el-card>
        <el-card shadow="always" style="margin-top: 20px">
            <highcharts :options="chartOptions" ref="chart"></highcharts>
        </el-card>
    </div>
</template>

<script>
    import i18n from "../../i18n";
    import {isEmptyObject} from "../../utils/util"
    import {Message} from "element-ui";

    export default {
        data() {
            return {
                settingList : [
                    {
                        "temperture":"",
                        "niandu": ""
                    },
                    {
                        "temperture":"",
                        "niandu": ""
                    },
                    {
                        "temperture":"",
                        "niandu": ""
                    }
                ],
                formInline: {
                    startTemperature: "",
                    endTemperature: ""
                },
                chartOptions: {
                    zoomType: 'x',
                    title: {
                        text: this.$i18n.tc("tool.chart.title")
                    },
                    yAxis: {
                        title: {
                            text: this.$i18n.tc("lab.naindu")
                        },
                        type: 'logarithmic'
                    },
                    xAxis: {
                        title: {
                            text: this.$i18n.tc("lab.temperture")
                        }
                    },
                    credits:{
                        enabled: false
                    },
                    series:  [{
                        name: i18n.t("lab.naindu"),
                        data: []
                    }],
                    tooltip: {
                        shared: true,
                        useHTML: true,
                        headerFormat:"",
                        pointFormatter: function () {
                            return this.x +  '<br/><span style="color: '+ this.series.color + '">\u25CF</span> '+
                                this.series.name + ': <b>'+ this.y +'</b><br/>'
                        }
                    },
                    exporting: {
                      buttons: {
                        contextButton: {
                          menuItems: ['viewFullscreen']
                        }
                      }
                    },
                    responsive: {
                        rules: [
                            {
                                condition: {
                                    maxWidth: 500
                                },
                                credits:{
                                    enabled: false // 禁用版权信息
                                }
                            }
                        ]
                    }
                }
            }
        },
        methods:{
            cal() {
                let flag = false;
                for (let i=0; i< this.settingList.length; i++) {
                    let setting = this.settingList[i];
                    if (isEmptyObject(setting.temperture) || isEmptyObject(setting.niandu)) {
                        flag = true;
                    }
                }

                if (flag) {
                    Message.error({
                        message: this.$i18n.tc("nianwen.dataCheckError"),
                        showClose: true
                    });
                    return;
                }

                if (isEmptyObject(this.formInline.startTemperature) || isEmptyObject(this.formInline.endTemperature)) {
                    Message.error({
                        message: this.$i18n.tc("nianwen.temperatureCheckError"),
                        showClose: true
                    });
                    return;
                }

                if (this.formInline.startTemperature > this.formInline.endTemperature) {
                    Message.error({
                        message: this.$i18n.tc("nianwen.temperatureCheckError"),
                        showClose: true
                    });
                    return;
                }

                if (isEmptyObject(this.settingList)) {
                    Message.error({
                        message: this.$i18n.tc("nianwen.dataCheckError"),
                        showClose: true
                    });
                    return;
                }

                // 两组数据处理
                // let v1 = this.settingList[0].niandu;
                // let t1 = this.settingList[0].temperture;
                // let v2 = this.settingList[1].niandu;
                // let t2 = this.settingList[1].temperture;
                //
                // let a = Math.log(Math.log(1.0 * v1 + 0.7));
                // let b = Math.log(Math.log(1.0 * v2 + 0.7));
                // b = (a - b) / (Math.log(1.0 * t2 + 273) - Math.log(1.0 * t1 + 273));
                // a = a + b * Math.log(1.0 * t1 + 273);
                //
                // this.chartOptions.series[0].data = [];
                // this.chartOptions.max = this.formInline.endTemperature  + 1;
                // this.chartOptions.min = this.formInline.startTemperature - 1;
                //
                // let tGap = (1.0 * (this.formInline.endTemperature - this.formInline.startTemperature + 2 )) / 500;
                // console.info(" tGap " + tGap);
                //
                // for (let t = 1.0 * this.formInline.startTemperature - 1; t <  1.0 * this.formInline.endTemperature  + 1; t = t + tGap) {
                //     this.chartOptions.series[0].data.push([1.0 * t,  Math.exp(Math.exp(a - b * Math.log(1.0 * t + 273))) - 0.7]);
                // }


                // 三组数据处理
                let v1 = Math.log(1.0 * this.settingList[0].niandu * 1000000);
                let t1 = 1.0 * this.settingList[0].temperture + 273;
                let v2 =  Math.log(1.0 * this.settingList[1].niandu * 1000000);
                let t2 = 1.0 * this.settingList[1].temperture + 273;
                let v3 =  Math.log(1.0 * this.settingList[2].niandu * 1000000);
                let t3 = 1.0 * this.settingList[2].temperture + 273;
                console.info("v1 is " + v1);
                console.info("t1 is " + t1);
                console.info("v2 is " + v2);
                console.info("t2 is " + t2);
                console.info("v3 is " + v3);
                console.info("t3 is " + t3);

                let m = (v2 - v1)/(v3 - v1);
                let n = (t2 - t1)/(t3 - t1);
                let m1 = m / n;
                let C = (m1 * t2 - t3 )/(m1 - 1);
                let B = (v1- v2) * (C - t1) * (C - t2) / (t1 - t2);
                let A = v1 - (v1 - v2)*(C - t2)/(t1 - t2);

                console.info("A is " + A);
                console.info("B is " + B);
                console.info("C is " + C);

                this.chartOptions.series[0].data = [];
                this.chartOptions.max = this.formInline.endTemperature  + 1;
                this.chartOptions.min = this.formInline.startTemperature - 1;

                let tGap = (1.0 * (this.formInline.endTemperature - this.formInline.startTemperature + 2 )) / 500;
                console.info(" tGap " + tGap);

                for (let t = 1.0 * this.formInline.startTemperature - 1; t <  1.0 * this.formInline.endTemperature  + 1; t = t + tGap) {
                    if (C - t - 273 === 0) {
                        continue
                    }
                    this.chartOptions.series[0].data.push([parseFloat((1.0 * t).toFixed(3)),  parseFloat((Math.exp(A + B / (C - t - 273))  / 1000000).toFixed(3))]);
                }
            }
        }
    }
</script>

<style scoped>

</style>
